package com.travel.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.travel.entity.User;
import com.travel.service.UserService;
import com.travel.service.impl.UserServiceImpl;

/**
 * Servlet implementation class PreserveServlet
 */
@WebServlet("/PreserveServlet")
@MultipartConfig
public class PersonCenter extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService=new UserServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PersonCenter() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String userId=request.getParameter("userid");
		String userName=request.getParameter("username");
		String userPassword=request.getParameter("userpassword");
		String userSex = request.getParameter("usersex");
		String userPhone=request.getParameter("userphone");
		String userAddress=request.getParameter("useraddress");
		String userbirthdayY=request.getParameter("YYYY");
		String userbirthdayM=request.getParameter("MM");
		String userbirthdayD=request.getParameter("DD");
		String userBirthday=userbirthdayY+"-"+userbirthdayM+"-"+userbirthdayD;
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(userBirthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		String userBirth = new SimpleDateFormat("yyyy-MM-dd").format(date);
		System.out.println(userName+"==="+userSex+"==="+userPhone+"==="+userAddress+"==="+userBirth);
		
		Part part = request.getPart("userpicture");
		
		 // 判断用户是否有上传文件
			if (part.getSize() == 0) {
				// 调用业务层 编辑hotel
				boolean update = userService.update(Integer.parseInt(userId), new User(Integer.parseInt(userId), userName, userPassword, Integer.parseInt(userSex), userBirth, userAddress,userPhone, ((User)session.getAttribute("user")).getUserPicture()));
				System.out.println( ((User)session.getAttribute("user")).getUserPicture());
				if (update) {
				    // 响应数据
				    PrintWriter out= response.getWriter();
				    out.print("success");
				}
			} else {
				
				// 有上传酒店的图片
				// 对上传的文件类型进行效验
				String allowImgType = "image/jpg,image/jpeg,image/png,image/gif";
				// 获取当前文件的内容类型
				String contentType = part.getContentType();
				if (allowImgType.indexOf(contentType) == -1) {
					// 文件类型不匹配
					System.out.println("上传文件类型不匹配");
					// 重定向注册页面,提示文件格式错误
					return;
				}else {
					// 获取上传的真实文件名
					String realFileName = part.getSubmittedFileName();
					System.out.println(realFileName);
					// 定义文件存储目录
					File file = new File("D:\\image\\scenic");
							
				
					StringBuffer filePath = new StringBuffer();
							
					filePath.append(file.getPath()).append(File.separator).append(realFileName);
					System.out.println(filePath);
					// 将文件写入指定位置
					part.write(filePath.toString());
							
					// 调用业务逻辑层
			
					boolean update = userService.update(Integer.parseInt(userId), new User(Integer.parseInt(userId), userName, userPassword, Integer.parseInt(userSex), userBirth, userAddress, ((User)session.getAttribute("user")).getUserPhone(), realFileName));

					 //response.setContentType("application/x-www-form-urlencoded;charset=utf-8");
					User user = new User(Integer.parseInt(userId), userName, userPassword, Integer.parseInt(userSex), userBirth, userAddress, userPhone, realFileName);
					session.setAttribute("user", user);
					if (update) {
					    // 响应数据
					    PrintWriter out= response.getWriter();
					    out.print("success");
					}
				}
			}
		
	}

}
